package com.zqxq.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject; 

/**
 * 根据项目接口使用的协议，实现相关的请求。
 *
 */
public class HttpClientHelper 
{
    public HttpClient doGet(String url) throws IOException//抛出异常
    {
    	JSONObject jsonResult; 
    	String GetEntity;
    	
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try 
		{
			HttpGet httpget = new HttpGet(url);	
			ResponseHandler<String> responseHandler = new ResponseHandler<String>() { 
	            public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {                
	                int status = response.getStatusLine().getStatusCode();
	                if (status == 200) {
	                    HttpEntity entity = response.getEntity();
	                    return EntityUtils.toString(entity);
	                } else {
	                    System.out.print("请求错误，状态码为：" + response.getStatusLine().getStatusCode());
	                    throw new ClientProtocolException("意外的状态返回: " + status);
	                } 
	           } 
	       };
	     //转换成json格式  
           jsonResult = JSONObject.fromObject(GetEntity); //该结果可用来提取任何想要的值 
		}
		finally 
		{
			httpclient.close();
			}
		return httpclient;
    }
}
